%%
%% This is file `bidi.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% bidi.dtx  (with options: `package')
%% 
%%   ____________________________
%% 
%%   The bidi package
%%   (C) 2007  François Charette
%%   License information appended
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bidi}
  [2008/07/01 v0.4  Bidirectional typesetting in XeLaTeX]
\newif\if@rlmain
\@rlmainfalse
\DeclareOption{rldocument}{\@rlmaintrue}
\ExecuteOptions{rldocument}
\ProcessOptions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifx\TeXXeTstate\undefined\else%
   \TeXXeTstate=1
\fi
\ifx\beginL\@undefined%
   \newlinechar`\^^J
   \typeout{^^JTo avoid this error message,^^J%
     run TeX--XeT or e-TeX engine instead of regular TeX.^^J}
   \errmessage{Right-to-Left Support Error: use TeX--XeT or e-TeX
     engine}%
\fi
\newif\if@rl
\AtBeginDocument{% Here we set the main document direction
  \if@rl\@rlmaintrue%
  \else\@rlmainfalse%
  \fi%
  %%\if@rlmain do something ??? \fi
}
\let\n@xt=\
\def\LR{\protect\pLR}%
\def\pLR{\protect\afterassignment\moreLR \let\n@xt= }
\def\moreLR{\bracetext \aftergroup\endL \beginL\@rlfalse}
\def\RL{\protect\pRL}
\def\pRL{\protect\afterassignment\moreRL \let\n@xt= }
\def\moreRL{\bracetext \aftergroup\endR \beginR\@rltrue}
\def\bracetext{\ifcat\n@xt{\else\ifcat\n@xt}\fi
  \errmessage{Missing left brace has been substituted}\fi \bgroup}
\everydisplay{\if@rl\aftergroup\beginR\fi }
\def\rl@everypar{\if@rl{\setbox\z@\lastbox\beginR\usebox\z@}\fi}
\let\o@everypar=\everypar
\newtoks\n@everypar
\n@everypar\expandafter{\the\o@everypar}
\o@everypar{\rl@everypar\the\n@everypar}
\let\everypar=\n@everypar
\def\@ensure@RL#1{\if@rl#1\else\RL{#1}\fi}
\def\@ensure@LR#1{\if@rl\LR{#1}\else#1\fi}
\def\@ensure@dir#1{\if@rl\RL{#1}\else{#1}\fi}
\let\@@TeX\TeX
\def\TeX{\@ensure@LR{\@@TeX}}
\let\@@LaTeX\LaTeX
\def\LaTeX{\@ensure@LR{\@@LaTeX}}
\let\@@LaTeXe\LaTeXe
\def\LaTeXe{\@ensure@LR{\@@LaTeXe}}
\@ifpackageloaded{xltxtra}{
\let\@@XeTeX\XeTeX
\def\XeTeX{\@ensure@LR{\@@XeTeX}}
}{}
%%VARIOUS LATEX MACROS
\def\list#1#2{%
  \ifnum \@listdepth >5\relax
    \@toodeep
  \else
    \global\advance\@listdepth\@ne
  \fi
  \rightmargin\z@
  \listparindent\z@
  \itemindent\z@
  \csname @list\romannumeral\the\@listdepth\endcsname
  \def\@itemlabel{#1}%
  \let\makelabel\@mklab
  \@nmbrlistfalse
  #2\relax
  \@trivlist
  \parskip\parsep
  \parindent\listparindent
  \advance\linewidth -\rightmargin
  \advance\linewidth -\leftmargin
  \if@rl
    \advance\@totalleftmargin \rightmargin
  \else
    \advance\@totalleftmargin \leftmargin
  \fi
  \parshape \@ne \@totalleftmargin \linewidth
  \ignorespaces}
%%
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\if@rl\rightskip\else\leftskip\fi #2\relax
      \if@rl\leftskip\else\rightskip\fi \@tocrmarg \parfillskip
      -\if@rl\leftskip\else\rightskip\fi
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\if@rl\rightskip\else\leftskip\fi \@tempdima
     \null\nobreak\hskip -\if@rl\rightskip\else\leftskip\fi
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor \beginL#5\endL}%
     \par}%
  \fi}
%%
\def\raggedright{%
  \let\\\@centercr
  \leftskip\z@skip\rightskip\@flushglue
  \parindent\z@\parfillskip\z@skip}
\let\@@raggedleft=\raggedleft
\let\@@raggedright=\raggedright
\renewcommand\raggedleft{\if@rl\@@raggedright%
                         \else\@@raggedleft\fi}
\renewcommand\raggedright{\if@rl\@@raggedleft%
                          \else\@@raggedright\fi}
\let\@@@underline=\underline
\def\underline#1{\@@@underline{\if@rl\RL{#1}\else #1\fi}}
\if@compatibility
   \let\undertext=\underline
\fi
\newif\if@rl@footnote
\if@rl\@rl@footnotetrue\else\@rl@footnotefalse\fi
\let\@@footnoterule=\footnoterule
\def\LRfootnoterule{\@@footnoterule}
%%bug?-->this causes new par to be set, hence hack with vskip :-{
\def\RLfootnoterule{\vskip -\baselineskip\hb@xt@\hsize{\hss\vbox{\@@footnoterule}}}
\def\setfootnoteRL{\@rl@footnotetrue}
\def\unsetfootnoteRL{\@rl@footnotefalse}
\def\setfootnoteLR{\unsetfootnoteRL}
\def\footnoterule{\if@rl@footnote\RLfootnoterule\else\LRfootnoterule\fi}
\def\setRL{\@rltrue\@rl@footnotetrue}
\def\unsetRL{\@rlfalse\@rl@footnotefalse}
\def\setLR{\unsetRL}
\long\def\@footnotetext#1{\insert\footins{%
    \if@rl@footnote\@rltrue\else\@rlfalse\fi
    \reset@font\footnotesize
    \interlinepenalty\interfootnotelinepenalty
    \splittopskip\footnotesep
    \splitmaxdepth \dp\strutbox \floatingpenalty \@MM
    \hsize\columnwidth \@parboxrestore
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}%
%%... also in minipages
\long\def\@mpfootnotetext#1{%
  \global\setbox\@mpfootins\vbox{%
    \if@rl@footnote\@rltrue\else\@rlfalse\fi
    \unvbox\@mpfootins
    \reset@font\footnotesize
    \hsize\columnwidth
    \@parboxrestore
    \protected@edef\@currentlabel
         {\csname p@mpfootnote\endcsname\@thefnmark}%
    \color@begingroup
      \@makefntext{%
        \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup
    }}
\@ifundefined{KOMAClassName}{%
\@ifclassloaded{memoir}{%
\long\def\@makecaption#1#2{\let\@memtempa\relax
  \ifdim\prevdepth>-99\p@ \vskip\abovecaptionskip
  \else \def\@memtempa{\vbox to\topskip{}}\fi
  \let\@contfnote\footnote \renewcommand{\footnote}[2][]{}
  \let\@contfmark\footnotemark \renewcommand{\footnotemark}[1][]{}
  \sbox\@tempboxa{\@contnfont #1\@contdelim \@conttfont #2}
  \let\footnote\@contfnote
  \let\footnotemark\@contfmark
  \ifdim\wd\@tempboxa<\linewidth \centering \fi
  \if@contcw
    \centering
    \parbox{\@contcwidth}{%
    \ifdim\wd\@tempboxa<\@contcwidth \centering \fi
  \fi
  \if@conthang
    \sbox\@tempboxa{\@contnfont #1\@contdelim}
    \@contpre%
    \@ensure@dir{\@contcstyle\hangindent=\wd\@tempboxa
         \noindent\box\@tempboxa\@memtempa \@conttfont #2}\par
  \else
    \if@contindent
      \@contpre%
      \@ensure@dir{\@contnfont #1\@contdelim}\@memtempa
      \@ensure@dir{\@contcstyle\hangindent=\@contindw
         \hangafter=\@ne\@conttfont #2}\par% <- v1.4
    \else
      \@contpre%
      \@ensure@dir{\@contnfont #1\@contdelim}\@memtempa
      \@ensure@dir{\ifdim\wd\@tempboxa<\linewidth
         \@contcshortstyle\else \@contcstyle\fi%  <- v1.4
 \@conttfont #2}\par
    \fi
  \fi
  \@contpost
  \if@contcw
    \par
    }  % end of the \parbox
  \fi
  \vskip\belowcaptionskip}
}% else we redefine \@makecaption for the standard classes
{\@ifundefined{@makecaption}{%
}{\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{\@ensure@dir{#1: #2}}%
  \ifdim \wd\@tempboxa >\hsize
    \@ensure@dir{#1: #2}\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
  \fi
  \vskip\belowcaptionskip}%
  }%
 }%
}% ELSE KOMASCRIPT
{\@ifclassloaded{scrlttr2}{}% do nothing for letter class
{\renewcommand{\@@makecaption}[3]{%
  \cap@margin
  \ifonelinecaptions
    \@tempcnta=\value{mpfootnote}\@tempcntb=\value{footnote}%
    \let\scr@tmp@footnotetext=\@footnotetext
    \let\@footnotetext=\@gobble
    \sbox\@tempboxa{%
      {\capfont\strut\ignorespaces
        #1{{\caplabelfont#2\captionformat}}%
        #3\unskip\strut}%
    }%
    \let\@footnotetext=\scr@tmp@footnotetext
    \let\scr@tmp@footnotetext=\undefined
    \setcounter{footnote}{\@tempcntb}%
    \setcounter{mpfootnote}{\@tempcnta}%
  \else
    \sbox\@tempboxa{\hspace*{2\cap@width}}%
  \fi
  \begingroup%
    \setlength{\@tempdima}{\cap@width}%
    \ifdim \wd\@tempboxa >\@tempdima
      \hb@xt@\hsize{%
        \setbox\@tempboxa\vbox{\hsize=\cap@width
          \ifdofullc@p
            \@ensure@dir{\capfont\@hangfrom{\scr@smashdp{\strut\ignorespaces
                  #1{{\caplabelfont#2\captionformat}}}}%
              {#3\unskip\strut\par}}%
          \else%
            \ifdim\cap@indent<\z@
              \@ensure@dir{\capfont\strut\ignorespaces
                #1{{\caplabelfont #2\captionformat\par}}%
                \noindent\hspace*{-\cap@indent}#3\unskip\strut\par}%
            \else%
              \if@capbreak
                \@ensure@dir{\capfont\strut\ignorespaces
                  #1{{\caplabelfont #2\captionformat\par}}%
                  \noindent\@hangfrom{\hspace*{\cap@indent}}{#3\par}}%
              \else
                \@ensure@dir{\capfont\@hangfrom{\scr@smashdp\strut\hspace*{\cap@indent}}%
                  {\hspace{-\cap@indent}\scr@smashdp{\ignorespaces#1%
                    {{\caplabelfont#2\captionformat}}}#3\unskip\strut\par}}%
              \fi
            \fi
          \fi
        }%
        \setlength{\@tempdima}{\ht\@tempboxa}%
        \addtolength{\@tempdima}{\dp\@tempboxa}%
        \addtolength{\@tempdima}{-\ht\strutbox}%
        \ht\@tempboxa\ht\strutbox
        \dp\@tempboxa\@tempdima
        \strut\cap@left
        \box\@tempboxa
        \cap@right\strut
}%
    \else
      \global \@minipagefalse
      \sbox\@tempboxa{%
        \@ensure@dir{\capfont\scr@smashdp{\strut\ignorespaces
          #1{{\caplabelfont#2\captionformat}}}%
          #3}%
      }%
      \hb@xt@\hsize{\strut\cap@left\box\@tempboxa\cap@right\strut}%
    \fi
  \endgroup}
}}%
\ifx\@textcolor\@undefined\else%
\AtBeginDocument{%
    \def\@textcolor#1#2#3{%
    \if@rl\PackageWarning{bidi}{%
    \textcolor will not work in RL mode if the textual argument spans more than one line.^^J%
    With XeTeX you should set color as a font feature instead.}%
      \beginL\protect\leavevmode{\color#1{#2}\beginR#3\endR}\endL%
      \else%
        \protect\leavevmode{\color#1{#2}#3}%
      \fi%
    }%
}%
\fi

%% 
%% Copyright (C) 2007-2008 by François Charette <firmicus at gmx dot net>
%% 
%% Distributable under the LaTeX Project Public License,
%% version 1.3c or higher (your choice). The latest version of
%% this license is at: http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained" (as per LPPL maintenance status)
%% by François Charette.
%% 
%% This work consists of the file  bidi.dtx
%%           and the derived files bidi.sty and bidi.pdf.
%% 
%%
%% End of file `bidi.sty'.
